package com.jjc.android.activity

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.toast.ToastUtils
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.UserManager
import com.jjc.android.adapter.TestedPersonAdapter
import com.jjc.android.base.BaseActivity
import com.jjc.android.dialog.MessageImageDialog
import com.jjc.android.entity.*
import com.jjc.android.net.SimpleHttpCallback
import com.jjc.android.util.ComplexSpannableStringUtils
import com.jjc.android.util.NoRefCopySpan
import com.jjc.android.util.SimpleOnRefreshLoadMoreListener
import com.jjc.android.util.paramEx
import com.jjc.android.view.onClick
import com.lzy.okgo.OkGo
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.zerobranch.layout.SwipeLayout
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class TestedPersonListActivity : BaseActivity() {

    override val layoutId: Int = R.layout.activity_operation_video
    private val smartRefreshLayout: SmartRefreshLayout by lazy { findViewById<SmartRefreshLayout>(R.id.smartRefreshLayout) }
    private val recyclerView: RecyclerView by lazy { findViewById<RecyclerView>(R.id.recyclerView) }
    private val homeBtn: ImageView by lazy { findViewById(R.id.homeBtn) }
    private lateinit var mAdapter: TestedPersonAdapter
    private val refreshLayoutDelegate by lazy {
        object : SimpleOnRefreshLoadMoreListener(){
            override fun invokeActivity(block: BaseActivity.() -> Unit) {
                this@TestedPersonListActivity.apply(block)
            }

            override fun loadPage(
                page: Int,
                actionSuccess: (Int, List<Any>?) -> Unit,
                actionError: (Int, String?) -> Unit
            ) {
                OkGo.post<LzyResponse<PageInfo<TestedPersonDto>>>(Constants.getFormList)
                    .paramEx(mutableMapOf("page" to page.toString(),"pagesize" to Constants.PAGE_SIZE.toString(),"needpage" to "1","formheaduuid" to "83ed64e98f6c4edbaf7b388d7691e57b","filters" to listOf<KeyValuePair>(
                        KeyValuePair("61xugqtzoyid5pw2",UserManager.userId())
                    )),true)
                    .tag(this)
                    .execute(object : SimpleHttpCallback<LzyResponse<PageInfo<TestedPersonDto>>>(false){
                        override fun onCallSuccess(response: LzyResponse<PageInfo<TestedPersonDto>>) {
                            actionSuccess.invoke(page,response.data?.data)
                        }

                        override fun onCallError(error: String?) {
                            super.onCallError(error)
                            actionError.invoke(page,error)
                        }
                    })
            }
        }
    }
    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(getString(R.string.tester_man_txt))
        recyclerView.layoutManager = LinearLayoutManager(this)
        mAdapter = TestedPersonAdapter()
        recyclerView.adapter = mAdapter
        refreshLayoutDelegate.init(smartRefreshLayout,recyclerView,false)
        homeBtn.apply {
            visibility = View.VISIBLE
            setImageResource(R.mipmap.beiceren_nav_addpeople)
            onClick {
                AddTestedPersonActivity.start(this@TestedPersonListActivity)
            }
        }
        mAdapter.setOnItemChildClickListener { _, view, position ->
            val info = mAdapter.getItem(position)?:return@setOnItemChildClickListener
            (view.parent?.parent as? SwipeLayout)?.let {
                if(it.isRightOpen){
                    it.close(true)
                    when(view.id){
                        R.id.editBtn->{
                            AddTestedPersonActivity.start(this@TestedPersonListActivity,info)
                        }
                        R.id.deleteBtn->{
                            MessageImageDialog.Builder(this)
                                .content(getString(R.string.remove_tester_all_dele_txt))
                                .okClick {
                                    OkGo.post<LzyResponse<Any>>(Constants.deleteFormRow)
                                        .paramEx(mutableMapOf("rowid" to info.id,"formheaduuid" to "83ed64e98f6c4edbaf7b388d7691e57b"),true)
                                        .tag(this)
                                        .execute(object : SimpleHttpCallback<LzyResponse<Any>>(activity = this@TestedPersonListActivity){
                                            override fun onCallSuccess(response: LzyResponse<Any>) {
                                                ToastUtils.show(getString(R.string.remove_success_txt))
                                                EventBus.getDefault().post(ChangeTesterEvent())
                                                mAdapter.remove(position)
                                            }
                                        })
                                }
                                .build()
                                .show()
                        }
                    }
                }
            }

        }

    }

    override fun showEmptyView(emptyText: String) {
        statusLayoutManager?.showEmptyLayout2(
            ComplexSpannableStringUtils.getBuilder(getString(R.string.no_tester_1_txt))
                .append(getString(R.string.no_device_2_txt))
                .setForegroundColor(ContextCompat.getColor(this,R.color.c_831cb5))
                .setClickSpan(NoRefCopySpan {
                    homeBtn.performClick()
                })
                .create()
        )
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: ChangeTesterEvent){
        refreshLayoutDelegate.requestRefresh()
    }

    override fun isHaveEventBus(): Boolean {
        return true
    }

}